﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using CRMFramework;
using System.Data;
using System.IO;


namespace CRMBiz
{
    public static class ErrorHandler
    {
        public static bool saveErrorLog(Exception exc, string userName)
        {


            bool flag = false;
            try
            {
                var para = new[] {
                DBhelper.SetParameter("@p_ErrorMessage",exc.ToString()),
                DBhelper.SetParameter("@p_url",HttpContext.Current.Request.Url.AbsoluteUri),
                DBhelper.SetParameter("@p_ipAddress",HttpContext.Current.Request.UserHostAddress),
                DBhelper.SetParameter("@p_operator",userName),
                DBhelper.SetParameter("@p_dateTime",System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                };

                DBhelper.ExecuteNonQuery("INSERT INTO crm_errorlog (ErrorMessage,FullUrl,IPaddress,Operator,Error_DateTime) values(@p_ErrorMessage,@p_url,@p_ipAddress,@p_operator,@p_dateTime)", para);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public static bool saveSMSEmailLog(string textToWrite)
        {
            bool flag = false;
            try
            {
                string path = HttpContext.Current.Server.MapPath("~/CrmLog/SendingEmailSms.log/log.txt");

                using (StreamWriter sw = (File.Exists(path)) ? File.AppendText(path) : File.CreateText(path))
                {
                    sw.WriteLine("----------------------------------------------------------------------------------------------");
                    sw.WriteLine(textToWrite);
                }
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }
    }
}
